package com.example.safelib.fragment


import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.example.baselib.dialog.MsgDialog
import com.example.baselib.fragment.BaseFragment
import com.example.baselib.utils.TimeUtils

import com.example.safelib.R
import com.example.safelib.SafeViewModel
import com.example.safelib.adapter.SafeReportAdapter
import kotlinx.android.synthetic.main.fragment_safe_report.*
import java.lang.Exception
import java.text.SimpleDateFormat
import java.util.*

/**
 * A simple [Fragment] subclass.
 */
class SafeReportFragment : BaseFragment<SafeViewModel>() {
    private lateinit var pvDate: TimePickerView
    private val reportAdapter :SafeReportAdapter by lazy { SafeReportAdapter(R.layout.safe_report_item) }
    override fun providerVMClass(): Class<SafeViewModel>? {
        return SafeViewModel::class.java
    }

    override fun contentLayout(): Int {
        return R.layout.fragment_safe_report
    }

    override fun initView() {
        val empty = LayoutInflater.from(context).inflate(R.layout.empty_item, null, false)
        empty.findViewById<TextView>(R.id.empty_msg).text="暂无数据\n重新选择日期试试"
        reportAdapter.emptyView=empty
        safe_report_date.text = TimeUtils.sdf.format(Calendar.getInstance().time)
        initDatePicker()
        safe_report_nick.text=viewModel.getUnitInfo()["unitname"]
        safe_report_rcy.layoutManager=LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false)
        safe_report_rcy.adapter=reportAdapter
        safe_report_date.setOnClickListener {
            pvDate.setDate(viewModel.reportDate)
            pvDate.show() }

        reportAdapter.setOnItemClickListener { adapter, view, position ->
            MsgDialog("开发中...").show(activity!!.supportFragmentManager,"")
        }
    }

    override fun initData() {
        safe_report_nick.text = viewModel.getUnitInfo()["unitname"]
        viewModel.getReoprtData().observe(this,androidx.lifecycle.Observer {
            reportAdapter.setNewData(it)
            safe_report_date.text = TimeUtils.sdf.format(viewModel.reportDate.time)
            safe_report_empty.visibility= View.INVISIBLE
        })

    }

    /**
     * @Description: [初始化时间选择器]
     * @Param:
     * @Return:
     */
    private fun initDatePicker() {
        val startDate= Calendar.getInstance()
        startDate.add(Calendar.YEAR,-2)
        pvDate = TimePickerBuilder(context,
            OnTimeSelectListener { date, _ ->
                viewModel.reportDate.time=date
                viewModel.loadReportList()
            })
            .setType(booleanArrayOf(true, true, true, false, false, false))//分别对应年月日时分秒，默认全部显示
            .isDialog(true)
            .setContentTextSize(14)
            .setLayoutRes(R.layout.pickerview_custom_time
            ) { v ->
                v?.findViewById<TextView>(R.id.time_title)?.text="选择报表日期"

                v?.findViewById<Button>(R.id.finish_btn)?.setOnClickListener {
                    pvDate.returnData()
                    pvDate.dismiss()

                }
                v?.findViewById<Button>(R.id.cancel_btn)?.setOnClickListener { pvDate.dismiss() }
            }
            .setTextColorCenter(resources.getColor(R.color.colorDBlue))
            .setItemVisibleCount(5)
            .setDate(Calendar.getInstance())
            .setRangDate(startDate, Calendar.getInstance())
            .setItemVisibleCount(9)
            .setLineSpacingMultiplier(2.6.toFloat())
            .setBackgroundId(R.color.colorBlack)
            .setLabel("", "", "", "", "", "")
            .isCenterLabel(false).build()
    }

    override fun requestError(it: Exception?) {
        safe_report_empty.visibility= View.VISIBLE
        if (it?.message=="暂无数据"){
            safe_report_empty.findViewById<TextView>(R.id.err_txt).text=it?.message
            safe_report_empty.findViewById<TextView>(R.id.err_msg).visibility=View.INVISIBLE
//            safe_report_empty.findViewById<ImageView>(R.id.err_img).visibility=View.INVISIBLE
        }else{
            super.requestError(it)
            safe_report_empty.findViewById<TextView>(R.id.err_msg).text = it?.message
        }
        safe_report_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadReportList() }
    }


}
